package com.yeu.action;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yeu.model.Evidence;
import com.yeu.util.ConnectionPool;

public class AudioAction extends ActionSupport implements ModelDriven<Evidence>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String, Boolean> boolResult;
	private List<Map<String,String>> result;
	private Evidence audio=new Evidence();
	
	public Map<String, Boolean> getBoolResult() {
		return boolResult;
	}
	
	public List<Map<String, String>> getResult() {
		return result;
	}

	public Evidence getAudio() {
		return audio;
	}

	public void setAudio(Evidence audio) {
		this.audio = audio;
	}

	@Override
	public Evidence getModel() {
		return this.audio;
	}
	
	public String add(){
		String sql="insert into AUDIO (AUDIOID,EVENTID,AUDIONAME,AUDIOCONTENT) values(AUDIO_SEQUENCE.nextval,?,?,?)";
		boolResult=ConnectionPool.executeUpdate(sql,audio.getEventId(),audio.getFileFileName(), audio.getFile());
		return "boolResult";
	}
	
	public String get() throws Exception{
		List<Map<String, Object>> tmpResult=new ArrayList<Map<String,Object>>();
		String sql="select * from AUDIO where AUDIOID=? and EVENTID=?";
		tmpResult=ConnectionPool.executeStreamQuery(sql, "AUDIOCONTENT", audio.getId(),audio.getEventId());
		for (Map<String, Object> map : tmpResult) {
			audio.setFileStream((InputStream)map.get("AUDIOCONTENT"));
			audio.setFileContentType("audio/mpeg");
			audio.setFileFileName((String)map.get("AUDIONAME"));
		}
		return INPUT;
	}
	
	public String exist(){
		String sql="select AUDIOID,AUDIONAME from AUDIO where EVENTID=?";
		result=ConnectionPool.executeQuery(sql, audio.getEventId());
		return SUCCESS;
	}
	
	public String delete(){
		String sql="delete from AUDIO where EVENTID=? and AUDIOID=?";
		boolResult=ConnectionPool.executeUpdate(sql,audio.getEventId(),audio.getId());
		return "boolResult";
	}
}
